import { jl2D } from "./jl";

/**
 * 使第一个实体水平方向旋转看向另一个实体
 * @param entity 第一个实体
 * @param other 另一个实体
 */
export function lookAtP(entity:GameEntity,other:GameEntity){
    let direction = other.position.sub(entity.position); //用位置的sub方法算出方向
    let face = direction.clone(); //用clone函数复制一个一样的位置对象
    let d = Quat.rotateY(Math.atan2(face.z, face.x)); //用初始化四元数的rotateY方法来旋转Y轴，用atan2函数来计算旋转弧度，这样我们就得到了一个玩家方向的四元数
    entity.meshOrientation = d; //设置子弹的旋转值为玩家方向
} 
export function creatZone(lo:GameVector3,hi:GameVector3,e:GameEntity):GameZone{
    const zone = world.addZone({ //添加一个炮台检测的区域
        selector: 'player', //区域内的物体搜索条件，这里为玩家
        bounds: {
            lo: e.position.sub({
                x: lo.x, y: lo.y, z: lo.z,
                
                set: function (x: number, y: number, z: number): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                copy: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                add: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                sub: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                mul: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                div: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                addEq: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                subEq: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                mulEq: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                divEq: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                dot: function (v: GameVector3): number {
                    throw new Error("Function not implemented.");
                },
                cross: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                scale: function (n: number): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                clone: function (): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                lerp: function (v: GameVector3, n: number): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                mag: function (): number {
                    throw new Error("Function not implemented.");
                },
                sqrMag: function (): number {
                    throw new Error("Function not implemented.");
                },
                towards: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                distance: function (v: GameVector3): number {
                    throw new Error("Function not implemented.");
                },
                normalize: function (): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                angle: function (v: GameVector3): number {
                    throw new Error("Function not implemented.");
                },
                max: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                min: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                exactEquals: function (v: GameVector3): boolean {
                    throw new Error("Function not implemented.");
                },
                equals: function (v: GameVector3): boolean {
                    throw new Error("Function not implemented.");
                }
                    
            }), //最低点，这里用位置的sub方法来实现一个位置减去另一个位置的功能
            hi: e.position.add({
                x: hi.x, y: hi.y, z: hi.z,
                set: function (x: number, y: number, z: number): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                copy: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                add: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                sub: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                mul: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                div: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                addEq: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                subEq: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                mulEq: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                divEq: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                dot: function (v: GameVector3): number {
                    throw new Error("Function not implemented.");
                },
                cross: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                scale: function (n: number): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                clone: function (): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                lerp: function (v: GameVector3, n: number): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                mag: function (): number {
                    throw new Error("Function not implemented.");
                },
                sqrMag: function (): number {
                    throw new Error("Function not implemented.");
                },
                towards: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                distance: function (v: GameVector3): number {
                    throw new Error("Function not implemented.");
                },
                normalize: function (): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                angle: function (v: GameVector3): number {
                    throw new Error("Function not implemented.");
                },
                max: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                min: function (v: GameVector3): GameVector3 {
                    throw new Error("Function not implemented.");
                },
                exactEquals: function (v: GameVector3): boolean {
                    throw new Error("Function not implemented.");
                },
                equals: function (v: GameVector3): boolean {
                    throw new Error("Function not implemented.");
                }
            }),
            intersect: function (b: GameBounds3): GameBounds3 {
                throw new Error("Function not implemented.");
            },
            contains: function (b: GameVector3): boolean {
                throw new Error("Function not implemented.");
            },
            containsBounds: function (b: GameBounds3): boolean {
                throw new Error("Function not implemented.");
            },
            intersects: function (b: GameBounds3): boolean {
                throw new Error("Function not implemented.");
            },
            set: function (lox: number, loy: number, loz: number, hix: number, hiy: number, hiz: number): GameBounds3 {
                throw new Error("Function not implemented.");
            },
            copy: function (b: GameBounds3): GameBounds3 {
                throw new Error("Function not implemented.");
            }
        },
    });
    return zone;
}
//63.8,6.6,250
//50.9,6.6,225.2
const scale = 1/16;
const Quat = new GameQuaternion(0, 0, 0, 1); //初始化四元数，用于旋转
/**
 * 使用炮弹攻击
 * @param mb 目标
 * @param pt 发射炮塔
 * @param mesh 子弹外形
 */
export function attack_by_pd(mb:GameEntity,pt:GameEntity,mesh:GameModelAssets) {
    let direction = mb.position.sub(pt.position); //用位置的sub方法算出方向
    let face = direction.clone(); //用clone函数复制一个一样的位置对象
    let d = Quat.rotateY(Math.atan2(face.z, face.x)); //用初始化四元数的rotateY方法来旋转Y轴，用atan2函数来计算旋转弧度，这样我们就得到了一个玩家方向的四元数
    pt.meshOrientation = d; //设置子弹的旋转值为玩家方向
    let bullet = world.createEntity({ //创建一个子弹实体
        mesh: mesh, //模型为子弹模型
        meshScale: new GameVector3(1/16, 1/16, 1/16), //大小为默认大小，[1/16, 1/16, 1/16]
        collides: false, //关闭碰撞
        gravity: false, //不受重力影响
        position: pt.position, //在玩家的位置生成子弹
        velocity: face.scale(0.15), //子弹的初速度是玩家方向，因为速度太快了，所以要用位置的scale方法来把xyz各乘上0.15
        meshOrientation: d.rotateY(Math.PI / 2), //子弹的头指向玩家，这里还需要用四元数的rotateY方法来旋转90度
    });
    return bullet;
}
export function spawnSDMS(){
    let fp = true;
    let zl = true;
    const sdms=world.createEntity({
        mesh: 'mesh/某人想要的模型（）.vb',
        meshScale: new GameVector3(scale,scale,scale),
        position: new GameVector3(62,6.5,226.5),
        collides: false,
        fixed: false,
        gravity: false,
        tags: ['canhurt'],
    })
    const bosszone = creatZone(new GameVector3(66,12,254),new GameVector3(47,2,222),sdms)
    const st = setInterval(()=>{
        let player=bosszone.entities()[0];
        if (player){
            if (jl2D({x:player.position.x,z:player.position.z},{x:sdms.position.x,z:sdms.position.z})<=6&&fp){
                fp=false;
                const motion=sdms.motion.loadByName('attack4');
                let direction = player.position.sub(sdms.position); //用位置的sub方法算出方向
                let face = direction.clone(); //用clone函数复制一个一样的位置对象
                let d = Quat.rotateY(Math.atan2(face.z, face.x)); //用初始化四元数的rotateY方法来旋转Y轴，用atan2函数来计算旋转弧度，这样我们就得到了一个玩家方向的四元数
                sdms.meshOrientation = d; //设置boss的旋转值为玩家方向
                motion.play();

                const pt=world.createEntity({
                    mesh: 'mesh/离子炮台.vb',
                    meshScale: new GameVector3(scale/1.5,scale/1.5,scale),
                    position: sdms.position,
                    collides: false,
                    fixed: false,
                    gravity: false,
                })
                const ptzone = creatZone(new GameVector3(66,12,254),new GameVector3(47,2,222),pt)

                ptzone.onEnter(({ entity }) => { //如果炮台检测的区域进入了玩家
                    entity.interval = setInterval(async () => { //每2秒执行一次，把这个程序放在entity.interval中
                        if (ptzone.entities().includes(entity)) { //如果玩家在这个区域里
                            const bullet=attack_by_pd(entity,pt,'mesh/炮弹.vb')
                            bullet.onEntityContact(({ other }) => { //每当子弹碰到另一个实体
        if (other.hp > 0 && other.hasTag('canhurt')) { //如果这个实体的血量大于0
            other.takeDamage_by_entity(10,sdms);
            bullet.destroy(); //攻击之后子弹消失，防止重复攻击
        }
    });

    await sleep(1500); //等1.5秒后
    if (!bullet.destroyed) { //如果这个子弹还没有清除
        bullet.destroy(); //清除子弹，防止子弹数量太多超过服务器负载
    }
                        }

                    }, 2000);
                });

                ptzone.onLeave(({ entity }) => {
                    if (entity.interval) clearInterval(entity.interval); //如果entity.interval这个程序存在，把每2秒执行一次的子弹程序取消，防止多个子弹程序一起发生
                });

                world.onDie(async ({ entity, damageType }) => { //当有玩家S亡时
                    if (entity.interval) clearInterval(entity.interval); //如果entity.interval这个程序存在，把每2秒执行一次的子弹程序取消，防止多个子弹程序一起发生
                    
                });
                // 5秒后销毁
                setTimeout(() => {
                    pt.destroy();
                    world.querySelectorAll('player').forEach((x)=>{
                    if (x.interval) clearInterval(x.interval);
                })
                }, 5000);
                setTimeout(() => {
                    fp=true;
                },6000)
            }
            else if (zl && !fp){
                zl=false;
                const motion=sdms.motion.loadByName('walk');
                motion.play();
                setTimeout(() => {
                    zl=true;
                },1/24*20*1000-300)
            }
        }
    },200)
    
}




